package com.aisile.sellergoods.service.impl;

import com.aisile.entity.PageResult;
import com.aisile.mapper.TbSpecificationMapper;
import com.aisile.mapper.TbSpecificationOptionMapper;
import com.aisile.pojo.TbSpecification;
import com.aisile.pojo.TbSpecificationExample;
import com.aisile.pojo.TbSpecificationOption;
import com.aisile.pojo.TbSpecificationOptionExample;
import com.aisile.pojogroup.Specification;
import com.aisile.sellergoods.service.SpecificationService;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;


@Service
public class SpecificationServiceImpl implements SpecificationService {
    @Autowired
    TbSpecificationMapper tbSpecificationMapper;

    @Autowired
    TbSpecificationOptionMapper tbSpecificationOptionMapper;

    @Override
    public PageResult findPage(int page, int rows, TbSpecification tbSpecification) {

        PageHelper.startPage(page,rows);
        String specName=tbSpecification.getSpecName();
        TbSpecificationExample example=new TbSpecificationExample();
        if(!StringUtils.isEmpty(specName)){
            example.createCriteria().andSpecNameLike("%"+specName+"%");

        }
        Page<TbSpecification> pageModel=(Page<TbSpecification>) tbSpecificationMapper.selectByExample(example);
        return new PageResult(pageModel.getTotal(),pageModel.getResult());
    }

    @Override
    public void save(Specification specification) {

        TbSpecification spec = specification.getSpec();//规格主表

        List<TbSpecificationOption> specOptionList = specification.getSpecOptionList();//规格选项数据

        //保存主表信息
        tbSpecificationMapper.insert(spec);
        //保存规格选项信息
        if(specOptionList!=null&&specOptionList.size()>0){

            for( TbSpecificationOption option:specOptionList){

                option.setSpecId(spec.getId());
                tbSpecificationOptionMapper.insert(option);


            }

        }
    }

    @Override
    public Specification findOne(long id) {
        //1.根据id 查询出规格的数据
        TbSpecification tbSpecification = tbSpecificationMapper.selectByPrimaryKey(id);

        //2.根据规格 id 查询出 规格对应的选项的数据
        TbSpecificationOptionExample example=new TbSpecificationOptionExample();
        example.createCriteria().andSpecIdEqualTo(id);

        List<TbSpecificationOption> tbSpecificationOptions = tbSpecificationOptionMapper.selectByExample(example);

        //3.封装到组合实体当中


        return new Specification(tbSpecification,tbSpecificationOptions);
    }

    @Override
    public void update(Specification specification) {

        TbSpecification spec = specification.getSpec();

        List<TbSpecificationOption> specOptionList = specification.getSpecOptionList();

        //1.更新spec信息

        tbSpecificationMapper.updateByPrimaryKey(spec);

        //2.根据specId 进行条件删除
        TbSpecificationOptionExample example=new TbSpecificationOptionExample();
        example.createCriteria().andSpecIdEqualTo(spec.getId());

        tbSpecificationOptionMapper.deleteByExample(example);

        //3.再添加 规格选项集合

        if(specOptionList!=null&&specOptionList.size()>0){

            for(TbSpecificationOption tbSpecificationOption:specOptionList){

                tbSpecificationOption.setSpecId(spec.getId());

                tbSpecificationOptionMapper.insert(tbSpecificationOption);
            }
        }
    }

    @Override
    public void dele(Long[] ids) {

        if(ids!=null&&ids.length>0){

            for(Long id:ids){

                tbSpecificationMapper.deleteByPrimaryKey(id);
                TbSpecificationOptionExample example=new TbSpecificationOptionExample();
                example.createCriteria().andSpecIdEqualTo(id);
                tbSpecificationOptionMapper.deleteByExample(example);
            }
        }
    }

    @Override
    public List<Map> findSpecList() {


        return tbSpecificationMapper.findSpecList();
    }

}
